<template>
  <div class="app-container">
 <el-row :gutter="20">
          <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="warehouseName"
            placeholder="请输入仓库"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="warehouseOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="仓库id" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入物料id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料全名" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入物料全名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料代码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入物料代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入库存数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料来源" prop="goodsFrom">
        <el-input
          v-model="queryParams.goodsFrom"
          placeholder="请输入物料来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除  0否  1是" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入是否删除  0否  1是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:stock:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:stock:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:stock:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存id" align="center" prop="stockId" />
      <el-table-column label="仓库id" align="center" prop="warehouseId" />
      <el-table-column label="仓库名称" align="center" prop="name" />
      <el-table-column label="物料id" align="center" prop="goodsId" />
      <el-table-column label="物料全名" align="center" prop="fullName" />
      <el-table-column label="物料代码" align="center" prop="goodsCode" />
      <el-table-column label="库存数量" align="center" prop="quantity" />
      <el-table-column label="物料来源" align="center" prop="goodsFrom" :formatter="goodsFromFormat" />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="updateUser" />
      <el-table-column label="是否删除  0否  1是" align="center" prop="isDel" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
 </el-col>
 </el-row>
    <!-- 添加或修改库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

       
        <el-form-item label="仓库名称" prop="warehouseId">
              <treeselect v-model="form.warehouseId" :options="warehouseOptions" :show-count="true" placeholder="请选择仓库名称" />
            </el-form-item>


 
 <el-form-item label="物料id" prop="goodsId">
          <el-select v-model="form.goodsOptions" placeholder="请选择物料">
            <el-option
              v-for="dict in goodsOptionss"
              :key="dict.goodsId"  
              :label="`${dict.goodsId}  ${dict.name}   ${dict.goodsCode}   ${dict.goodsModel}`"
              :value="`${dict.goodsId}|${dict.name}|${dict.goodsCode}|${dict.goodsModel}`"
            ></el-option>
          </el-select>
        </el-form-item>



        <el-form-item label="库存数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存数量" />
        </el-form-item>


      
 <el-form-item label="物料来源" prop="goodsFrom">
          <el-select v-model="form.goodsFrom" placeholder="请选择物料来源">
            <el-option
              v-for="dict in goodsFromOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>









        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listgood,listStock, getStock, delStock, addStock, updateStock, exportStock } from "@/api/base/stock";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/base/warehouse";
import { parseJson } from "@/utils/index";
export default {
  name: "Stock",
  components: {Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      stockList: [],
      //物料
      goodsOptionss:[],
            // 仓库名称
      warehouseName: undefined,
            // 仓库树选项
      warehouseOptions: undefined,
      // 弹出层标题
      title: "",
      //goodssel
      goodsel:'',
      // 是否显示弹出层
      open: false,
      // 物料来源字典
      goodsFromOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: undefined,
        name: undefined,
        goodsId: undefined,
        fullName: undefined,
        goodsCode: undefined,
        quantity: undefined,
        goodsFrom: undefined,
        createUser: undefined,
        updateUser: undefined,
        isDel: undefined,
        goodsOptions:undefined,
      },
      // 表单参数
      form: {},
        defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
    watch: {
    // 根据名称筛选部门树
    warehouseName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
      this.getTreeselect();
    this.getDicts("base_stock_goods_from").then(response => {
      this.goodsFromOptions = response.data;
    });
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
   //查询物料
    getlistgood(){
         listgood().then(response => {
        this.goodsOptionss = response.rows;
       // console.log(response.rows)
        
      });
    },
      /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.warehouseOptions = response.data;
        //console.log(response.data)
      });
    },
 // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
      // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.warehouseId = data.id;
      this.getList();
     // console.log(this.queryParams.warehouseId)
    },























    // 物料来源字典翻译
    goodsFromFormat(row, column) {
      return this.selectDictLabel(this.goodsFromOptions, row.goodsFrom);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockId: undefined,
        warehouseId: undefined,
        name: undefined,
        goodsId: undefined,
        fullName: undefined,
        goodsCode: undefined,
        quantity: undefined,
        goodsFrom: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        remark: undefined,
        goodsOptions:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stockId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getlistgood();
      this.reset();
      this.open = true;
      this.title = "添加库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockId = row.stockId || this.ids
      getStock(stockId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存";
      });
    },

  

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        

        if (valid) {

          //处理物料
            let tmp=this.form.goodsOptions.split('|');
            this.form.goodsId=parseInt(tmp[0]);
            this.form.fullName=tmp[1];
            this.form.goodsCode=tmp[2];
            this.form.goodsModel=tmp[3];
            //处理仓库
            let whname=parseJson(this.warehouseOptions,this.form.warehouseId);
                //console.log(whname)
                //console.log(this.warehouseOptions)
                //console.log(this.form.warehouseId)

                this.form.name=whname;



          if (this.form.stockId != null) {
            updateStock(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {          
            addStock(this.form).then(response => {
              
               
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stockIds = row.stockId || this.ids;
      this.$confirm('是否确认删除库存编号为"' + stockIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStock(stockIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库存数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStock(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
